#include "editanimationwidget.h"
#include "ui_editanimationwidget.h"
#include "../../gameobjects/animation.h"

EditAnimationWidget::EditAnimationWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EditAnimationWidget)
{
    ui->setupUi(this);

    resetParameters(0);

    connect(ui->lineEditHPattern, SIGNAL(textChanged(QString)),
                                this, SLOT(patternChanged()));
    connect(ui->lineEditVPattern, SIGNAL(textChanged(QString)),
                                this, SLOT(patternChanged()));
    connect(ui->lineEditNumFrames, SIGNAL(textChanged(QString)),
                                this, SIGNAL(parametersChanged()));
    connect(ui->lineEditSpeed, SIGNAL(textChanged(QString)),
                                this, SIGNAL(parametersChanged()));

    connect(ui->pushButtonStartStop, SIGNAL(clicked()),
            this, SIGNAL(startStopAnimation()));
    connect(ui->pushButtonRewind, SIGNAL(clicked()),
            this, SIGNAL(rewind()));
    connect(ui->pushButtonGoToEnd, SIGNAL(clicked()),
            this, SIGNAL(goToEnd()));
    connect(ui->pushButtonStepBackward, SIGNAL(clicked()),
            this, SIGNAL(stepBackward()));
    connect(ui->pushButtonStepForward, SIGNAL(clicked()),
            this, SIGNAL(stepForward()));

}

EditAnimationWidget::~EditAnimationWidget()
{
    delete ui;
}

QWidget *EditAnimationWidget::previewWidget() const
{
    return ui->widgetPreview;
}

QWidget *EditAnimationWidget::animationControl() const
{
    return ui->widgetAnimControl;
}

void EditAnimationWidget::getParams(int &xPattern, int &yPattern, int &numFrames, bool &bLoop, int &speed)
{
    xPattern = ui->lineEditHPattern->text().toInt();
    yPattern = ui->lineEditVPattern->text().toInt();
    numFrames = ui->lineEditNumFrames->text().toInt();
    speed = ui->lineEditSpeed->text().toInt();
}


void EditAnimationWidget::patternChanged()
{
    int xPattern = ui->lineEditHPattern->text().toInt();
    int yPattern = ui->lineEditVPattern->text().toInt();

    if(xPattern == 0)
    {
        xPattern = 1;
        ui->lineEditHPattern->blockSignals(true);
        ui->lineEditHPattern->setText("1");
        ui->lineEditHPattern->blockSignals(false);
    }

    if(yPattern == 0)
    {
        yPattern = 1;
        ui->lineEditVPattern->blockSignals(true);
        ui->lineEditVPattern->setText("1");
        ui->lineEditVPattern->blockSignals(false);
    }

    if(ui->checkBoxCalculate->isChecked())
    {
        ui->lineEditNumFrames->setText(QString("%1").arg(xPattern*yPattern));
    }
    else
    {
        emit parametersChanged();
    }
}

void EditAnimationWidget::resetParameters(QPointer<Animation> anim)
{
    blockSignals(true);

    if(anim && anim->texture())
    {
        ui->pushButtonStartStop->setEnabled(true);
        ui->pushButtonGoToEnd->setEnabled(true);
        ui->pushButtonRewind->setEnabled(true);
        ui->pushButtonStepBackward->setEnabled(true);
        ui->pushButtonStepForward->setEnabled(true);

        ui->lineEditHPattern->setEnabled(true);
        ui->lineEditVPattern->setEnabled(true);
        ui->lineEditNumFrames->setEnabled(true);
        ui->lineEditSpeed->setEnabled(true);
        ui->checkBoxLoop->setEnabled(true);
        ui->checkBoxCalculate->setEnabled(true);

        ui->lineEditHPattern->setText(QString("%1").arg(anim->xPattern()));
        ui->lineEditVPattern->setText(QString("%1").arg(anim->yPattern()));
        ui->lineEditNumFrames->setText(QString("%1").arg(anim->numFrames()));
        ui->lineEditSpeed->setText("1");
        ui->checkBoxLoop->setChecked(true);
    }
    else
    {
        ui->pushButtonStartStop->setEnabled(false);
        ui->pushButtonGoToEnd->setEnabled(false);
        ui->pushButtonRewind->setEnabled(false);
        ui->pushButtonStepBackward->setEnabled(false);
        ui->pushButtonStepForward->setEnabled(false);

        ui->lineEditHPattern->setEnabled(false);
        ui->lineEditVPattern->setEnabled(false);
        ui->lineEditNumFrames->setEnabled(false);
        ui->lineEditSpeed->setEnabled(false);
        ui->checkBoxLoop->setEnabled(false);
        ui->checkBoxCalculate->setEnabled(false);

        ui->lineEditHPattern->setText("1");
        ui->lineEditVPattern->setText("1");
        ui->lineEditNumFrames->setText("1");
        ui->lineEditSpeed->setText("1");
        ui->checkBoxLoop->setChecked(true);
    }

    ui->checkBoxCalculate->setChecked(true);

    blockSignals(false);
}
